<template>
  <AddOrEditDialog
    ref="addOrEditDialogRef"
    :form="form"
    title="学生"
    :formHeader="formHeader"
    :url="$urls.API_URL_BACKSTAGE_SCHOOLSTUDENT_MANAGE"
    :action="action"
    @afterSubmitSuccess="afterSubmitSuccess"
    >

    <template #classId>
      <el-autocomplete
        v-model="studentClassId"
        :fetch-suggestions="querySearch"
        clearable=true
        @select="handleSelect"
        placeholder="输入所属行政班"
      >
      </el-autocomplete>
    </template>

  </AddOrEditDialog>
</template>

<script lang="ts" setup>
// 引入添加编辑模块
import AddOrEditDialog from '@/components/base/table/form/AddOrEditDialog.vue';
import {
  computed,
  defineExpose, defineProps, getCurrentInstance, onMounted, reactive, ref, withDefaults,
} from 'vue';
// 引入表格相关
import { TableColumn, FormHeader, TableData } from '@/types/interface/base_interface';
// 引入学生对象
import { SchoolCourse, SchoolStudent } from '@/types/interface/school_interface';
import { userSexDict } from '@/api/dict';
import Request from '@/api/request/request';

const { proxy } = getCurrentInstance();

// 接收父容器参数
interface Props {
  table: TableData, // 偷懒写法
  action?: string, // 表单的处理类型，post表示添加，put表示编辑
}

const props = withDefaults(defineProps<Props>(), {
  action: 'post',
});

// 定义表单元素
const formHeader = ref<FormHeader[]>([
  {
    label: '所属行政班',
    prop: 'classId',
    type: 'custom',
    rules: [
      {
        required: true,
        message: '不能为空',
        trigger: 'blur',
      },
    ],
  },
  {
    label: '姓名',
    prop: 'name',
    rules: [
      {
        required: true,
        message: '不能为空',
        trigger: 'blur',
      },
    ],
  },
  {
    label: '性别',
    prop: 'sex',
    type: 'select',
    dict: userSexDict,
  },
  {
    label: '学号',
    prop: 'studentNumber',
    type: 'number',
    rules: [{
      required: true,
      message: '请输入学号',
      trigger: 'blur',
    },
    ],
  },
  {
    label: '联系电话',
    prop: 'tel',
    type: 'string',
  },
  {
    label: '籍贯',
    prop: 'nativePlace',
    type: 'string',
    rules: [{
      required: true,
      message: '请输入籍贯',
      trigger: 'blur',
    },
    ],
  },
  {
    label: '家庭住址',
    prop: 'address',
    type: 'string',
    rules: [{
      required: true,
      message: '请输入家庭住址',
      trigger: 'blur',
    },
    ],
  },
  {
    label: '身份证号',
    prop: 'identityCard',
    type: 'string',
    rules: [{
      required: true,
      message: '请输入身份证号',
      trigger: 'blur',
    },
    {
      min: 17,
      max: 19,
      message: '长度在 17 到 19 个字符',
      trigger: 'blur',
    },
    ],
  },
  {
    label: '民族',
    prop: 'national',
    type: 'string',
    rules: [{
      required: true,
      message: '请输入民族',
      trigger: 'blur',
    },
    ],
  },
  {
    label: '兴趣爱好',
    prop: 'hobby',
    type: 'textarea',
  },
  {
    label: '技能特长',
    prop: 'skill',
    type: 'textarea',
  },
]);

const studentClassId = ref<string>('');
// 行政班
const classId = ref();
const classList = ref<any>([]);
const DoclassId = async () => {
  console.log(classId.value);
  classList.value = await new Request(proxy.$urls.API_URL_BACKSTAGE_SCHOOLCLASS_MANAGE, null, {
    params: {
      name: classId.value,
    },
  }).get();
};
// 可以自定义处理方法，也可以使用element-plus定义的方法
const filter = (arr:[]) => {
  const list: [] = [];
  let obj = {};
  arr.forEach((item:any) => {
    obj = {
      value: item.name,
      id: item.id,
    };
    list.push(obj);
  });
  return list;
};

const querySearch = async (queryString: string, cb: any) => {
  let resData:any = await new Request(proxy.$urls.API_URL_BACKSTAGE_SCHOOLCLASS_MANAGE, null, {
    params: {
      name: queryString,
    },
  }).get();
  if (resData.code === 200) {
    const { data } = resData.data;
    const list = filter(data);
    console.log(list);
    cb(list);
  }
};

onMounted(() => {
  DoclassId();
});
// 表单对象
const form = ref<SchoolStudent>();
const addOrEditDialogRef = ref(); // 表单ref对象
let formInit:SchoolStudent; // 记录表单第一次传入的值,用于表单重置恢复初始值使用
// 打开对话框，供父组件调用，传入表单对象
const openDialog = (schoolStudent: SchoolStudent) => {
  formInit = { ...schoolStudent }; // 获取初始值，这里要做深拷贝，避免与form对象引用的地址一致，导致无法恢复初始值
  form.value = schoolStudent;
  // 打开对话框
  addOrEditDialogRef.value!.openDialog();
};
// 提交表单成功后的钩子函数
const afterSubmitSuccess = () => {
  // 让表单恢复为传入对象时的初始值，注意要深度拷贝，避免初始值收到污染
  form.value = { ...formInit };
};
// 暴露组件给父容器使用
defineExpose({
  openDialog,
});
</script>
<style lang="scss" scoped>

</style>
